package simple.framework.util;

import simple.framework.util.dbc.*;
import simple.framework.util.image.*;
import java.util.regex.*;

public class upfiles
{
  public conf conf;

  private void CreateDatabaseNote(String argGenre, String argFilename, String argField, int argForeback, String argUserName)
  {
    String tGnere = argGenre;
    String tFilename = argFilename;
    String tField = argField;
    int tForeback = argForeback;
    String tUserName = argUserName;
    String tdatabase = cls.getString(conf.sp.itake("global.config.sys->upload-ndatabase", "cfg"));
    String tfpre = cls.getString(conf.sp.itake("global.config.sys->upload-nfpre", "cfg"));
    String tsqlstr = "insert into " + tdatabase + " (";
    tsqlstr += cls.cfnames(tfpre, "genre") + ",";
    tsqlstr += cls.cfnames(tfpre, "filename") + ",";
    tsqlstr += cls.cfnames(tfpre, "field") + ",";
    tsqlstr += cls.cfnames(tfpre, "foreback") + ",";
    tsqlstr += cls.cfnames(tfpre, "username") + ",";
    tsqlstr += cls.cfnames(tfpre, "time");
    tsqlstr += ") values (";
    tsqlstr += "'" + cls.getLeft(encode.addslashes(tGnere), 50) + "',";
    tsqlstr += "'" + cls.getLeft(encode.addslashes(tFilename), 255) + "',";
    tsqlstr += "'" + cls.getLeft(encode.addslashes(tField), 50) + "',";
    tsqlstr += tForeback + ",";
    tsqlstr += "'" + cls.getLeft(encode.addslashes(tUserName), 255) + "',";
    tsqlstr += "'" + cls.getDate() + "'";
    tsqlstr += ")";
    dbc tDbc = db.newInstance(conf);
    tDbc.Execute(tsqlstr);
  }

  public void UpdateDatabaseNote(String argGenre, String argFilename, String argField, int argId)
  {
    String tGnere = argGenre;
    String tFilename = argFilename;
    String tField = argField;
    int tId = argId;
    dbc tDbc = db.newInstance(conf);
    String tdatabase = cls.getString(conf.sp.itake("global.config.sys->upload-ndatabase", "cfg"));
    String tfpre = cls.getString(conf.sp.itake("global.config.sys->upload-nfpre", "cfg"));
    String tsqlstr = "update " + tdatabase + " set " + cls.cfnames(tfpre, "valid") + "=0," + cls.cfnames(tfpre, "vlreason") + "=2 where " + cls.cfnames(tfpre, "fid") + "=" + tId + " and " + cls.cfnames(tfpre, "genre") + "='" + tGnere + "' and " + cls.cfnames(tfpre, "field") + "='" + tField + "'";
    tDbc.Execute(tsqlstr);
    String tnFilename;
    String[] tAry = tFilename.split(Pattern.quote("|"));
    for (int ti = 0; ti < tAry.length; ti ++)
    {
      tnFilename = cls.getSafeString(tAry[ti]);
      if (!cls.isEmpty(tnFilename)) tDbc.Execute("update " + tdatabase + " set " + cls.cfnames(tfpre, "fid") + "=" + tId + "," + cls.cfnames(tfpre, "valid") + "=1 where " + cls.cfnames(tfpre, "genre") + "='" + tGnere + "' and " + cls.cfnames(tfpre, "field") + "='" + tField + "' and " + cls.cfnames(tfpre, "filename") + "='" + tnFilename + "'");
    }
  }

  public void DeleteDatabaseNote(String argGenre, String argIdary)
  {
    String tGnere = argGenre;
    String tIdary = argIdary;
    String tdatabase = cls.getString(conf.sp.itake("global.config.sys->upload-ndatabase", "cfg"));
    String tfpre = cls.getString(conf.sp.itake("global.config.sys->upload-nfpre", "cfg"));
    String tidfield = cls.cfnames(tfpre, "id");
    if (cls.cidary(tIdary))
    {
      dbc tDbc = db.newInstance(conf);
      tDbc.Execute("update " + tdatabase + " set " + cls.cfnames(tfpre, "valid") + "=0," + cls.cfnames(tfpre, "vlreason") + "=1 where " + cls.cfnames(tfpre, "genre") + "='" + tGnere + "' and " + cls.cfnames(tfpre, "fid") + " in (" + tIdary + ")");
    }
  }

  public String uploadHTML(String argName)
  {
    String tName = argName;
    String tfid = cls.getSafeString(conf.getRequestUsParameter("fid"));
    String tfnid = cls.getSafeString(conf.getRequestUsParameter("fnid"));
    String tfmode = cls.getSafeString(conf.getRequestUsParameter("fmode"));
    String tfuptype = cls.getSafeString(conf.getRequestUsParameter("fuptype"));
    String tfupmaxsize = cls.getSafeString(conf.getRequestUsParameter("fupmaxsize"));
    String tmpstr = conf.sp.itake("global.tpl_common." + tName, "tpl");
    tmpstr = tmpstr.replace("{$fid}", encode.htmlencode(tfid));
    tmpstr = tmpstr.replace("{$fnid}", encode.htmlencode(tfnid));
    tmpstr = tmpstr.replace("{$fmode}", encode.htmlencode(tfmode));
    tmpstr = tmpstr.replace("{$fname}", encode.htmlencode(tName));
    tmpstr = tmpstr.replace("{$fuptype}", encode.htmlencode(tfuptype));
    tmpstr = tmpstr.replace("{$fupmaxsize}", encode.htmlencode(tfupmaxsize));
    tmpstr = tmpstr.replace("{$errstate}", "-2");
    tmpstr = tmpstr.replace("{$fullfilename}", "");
    tmpstr = tmpstr.replace("{$-fuptype}", "");
    tmpstr = tmpstr.replace("{$-fupmaxsize}", "");
    tmpstr = conf.sp.creplace(tmpstr);
    return tmpstr;
  }

  public String uploadFiles(String argName, int argForeback, String argUserName)
  {
    String tName = argName;
    int tForeback = argForeback;
    String tUserName = argUserName;
    int tErrState = -1;
    int tLimitLength = 0;
    String tnupType = "";
    String tmpstr = "";
    String tFullFileName = "";
    String tfid = cls.getSafeString(conf.getRequestUsParameter("fid"));
    String tfnid = cls.getSafeString(conf.getRequestUsParameter("fnid"));
    String tfmode = cls.getSafeString(conf.getRequestUsParameter("fmode"));
    String tfname = cls.getSafeString(conf.getRequestUsParameter("fname"));
    String tfuptype = cls.getSafeString(conf.getRequestUsParameter("fuptype"));
    String tfupmaxsize = cls.getSafeString(conf.getRequestUsParameter("fupmaxsize"));
    String tNotefid = tfnid;
    if (cls.isEmpty(tNotefid)) tNotefid = tfid;
    jupload tJUpload = new jupload(conf, tName);
    String tJUploadFileName = tJUpload.getFileName();
    int tJUploadContentLength = tJUpload.getContentLength();
    if (tJUploadContentLength <= 0) tErrState = 0;
    else
    {
      tLimitLength = cls.getNum(conf.sp.itake("config.nupmaxsize", "cfg"), -1);
      if (tLimitLength == -1) tLimitLength = cls.getNum(conf.sp.itake("global.config.nupmaxsize", "cfg"), -1);
      int tLimitLength2 = cls.getNum(tfupmaxsize, -1);
      if (tLimitLength2 != -1 && tLimitLength2 < tLimitLength) tLimitLength = tLimitLength2;
      if (tJUploadContentLength > tLimitLength) tErrState = 1;
      else
      {
        String tFileType = cls.getLRStr(tJUploadFileName, ".", "right");
        tFileType = tFileType.toLowerCase();
        tnupType = cls.getString(conf.sp.itake("config.nuptype", "cfg"));
        String tnupType2 = cls.getString(tfuptype);
        if (!cls.isEmpty(tnupType2) && cls.cinstrs(tnupType, tnupType2, ".")) tnupType = tnupType2;
        if (!cls.cinstr(tnupType, tFileType, ".")) tErrState = 2;
        else
        {
          String tFilePath = cls.getString(conf.sp.itake("config.nuppath", "cfg")) + cls.formatDate(cls.getDate(), 5);
          String tFullPath = conf.application.getRealPath(conf.getMapPath(tFilePath)).toString();
          if (!conf.common.directoryCreate(tFullPath)) tErrState = 3;
          else
          {
            String tFileName = cls.formatDate(cls.getDate(), 20) + cls.getRandomString(2) + "." + tFileType;
            Boolean tSaveBoolean = tJUpload.saveAs(tFullPath + "/" + tFileName);
            tFullFileName = tFilePath + "/" + tFileName;
            if (tSaveBoolean == true && tfmode != "1") CreateDatabaseNote(conf.getNGenre(), tFullFileName, tNotefid, tForeback, tUserName);
          }
        }
      }
    }
    if (tfname.indexOf(".") != -1) tmpstr = conf.sp.itake(tfname, "tpl");
    else tmpstr = conf.sp.itake("global.tpl_common." + tfname, "tpl");
    tmpstr = tmpstr.replace("{$fid}", encode.htmlencode(tfid));
    tmpstr = tmpstr.replace("{$fnid}", encode.htmlencode(tfnid));
    tmpstr = tmpstr.replace("{$fmode}", encode.htmlencode(tfmode));
    tmpstr = tmpstr.replace("{$fname}", encode.htmlencode(tfname));
    tmpstr = tmpstr.replace("{$fuptype}", encode.htmlencode(tfuptype));
    tmpstr = tmpstr.replace("{$fupmaxsize}", encode.htmlencode(tfupmaxsize));
    tmpstr = tmpstr.replace("{$errstate}", cls.toString(tErrState));
    tmpstr = tmpstr.replace("{$fullfilename}", encode.htmlencode(tFullFileName));
    tmpstr = tmpstr.replace("{$-fuptype}", encode.htmlencode(tnupType));
    tmpstr = tmpstr.replace("{$-fupmaxsize}", encode.htmlencode(cls.formatByte(cls.toString(tLimitLength))));
    tmpstr = conf.sp.creplace(tmpstr);
    return tmpstr;
  }

  public upfiles(conf argConf)
  {
    conf = argConf;
  }
}